layui.extend({
    selectN: '/layui_extends/selectN',
    selectM: '/layui_extends/selectM',
    baseConfig : "/js/pages/baseConfig"
}).use(['form','layer','laydate','table','laytpl','element','util','upload','selectN','selectM','baseConfig'],function(){

    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        table = layui.table,
        upload = layui.upload,
        laydate = layui.laydate,
        selectN = layui.selectN,
        baseConfig = layui.baseConfig,
        selectM = layui.selectM;
    var util = layui.util;

    var tableIns = table.render({
        elem: '#list',
        url : '/consumer/listForMooncard',
        text: { none: "Aucun résultat"},
        cellMinWidth : 95,
        height : "full",
        page: true, //开启分页
        limit : 20,
        limits : [20,50,70,100],
 //       toolbar: true, //开启头部工具栏，并为其绑定左侧模板
  //      defaultToolbar: ['filter'],
        totalRow : true, //开启合计行
        id : "listTable",
        cols : [[
            {field: 'dateDeDepense', title: "Date", align:"center", templet : function( d){
                return baseConfig.formatDate( d.dateDeDepense);
            }},
            {field: 'fournisseur', title: "Contrepartie", align:"center", templet : function( d){
                return "<div style=\"overflow:hidden;white-space:nowrap;text-overflow:ellipsis;\">" + d.fournisseur + "</div>";
            }},
            {field: 'jobNumber', title: "ID", align:"center"},
            {field: 'carteIdentifiant', title: "No. Carte", align:"center"},
            {field: 'sourceFrom', title: "Méthode", align:"center", templet : function( d) {
                var str = "";
                if( d.sourceFrom == "MOONCARD_EXCEL"){
                    str = "Carte";
                }
                return str;
            }},
            {field: 'montantTTC', title: "Montant TTC", align:"center", totalRow: true, totalRowText: "总计：",templet : function( d) {
                var str = parseFloat( d.montantTTC).toFixed(2) + " €";
                return "<div style='width: 40%;text-align: right;margin-right: 30%'>" + str + "</div>"
            }}
        ]],
        response : {
            statusCode: 200 //规定成功的状态码，默认：0
        },
        parseData : function( res){ //res 即为原始返回的数据
            return {
                "code": res.code, //解析接口状态
                "msg": res.msg, //解析提示文本
                "count": res.data.count, //数据条数
                "data": res.data.data //解析数据列表
            };
        }
    });

    //上传excel文件
    upload.render({
        elem: '#import_excel_btn',
        url: '/file/upload?type=consumerImportExcel',
        method : "post",
        accept : "file",
        exts : "xlsx|xls",
        done: function(res){
            if( res.code == 200){
                var excelPath = res.data.filePath;
                var index = layer.msg('导入中，请稍候....',{ icon: 16, time:false, shade:0.8});
                //调用excel解析接口
                $.post( "/mooncardClient/importFromExcel",{
                    "filePath" : excelPath
                },function( res){
                    layer.close( index);
                    if( res.code == 200){
                        var repeats = res.data.repeatDatas;
                        var successCount = res.data.successCount;
                        var errorDatas = res.data.errorDatas;
                        var msg = "导入成功,本次成功导入[" + successCount + "]条数据";
                        if( repeats && repeats.length != 0){
                            msg += "，出现条[" + repeats.length + "]重复导入的数据";
                        }
                        if( errorDatas && errorDatas.length != 0){
                            var errStrs = "";
                            for( var i = 0 ; i < errorDatas.length ; i++){
                                if( i != 0){
                                    errStrs += ",";
                                }
                                errStrs += errorDatas[i].rowNum;
                            }
                            msg += "，出现表格数据{" + errStrs + "}这几行因为格式错误导入失败的数据";
                        }
                        top.layer.alert( msg);
                        tableIns.reload();
                    }else if( res.code == 70000){
                        top.layer.alert( res.msg);
                    }else{
                        top.layer.alert( res.msg ? res.msg : "导入失败");
                    }
                });
            }
        }
    });

    $("#import_excel_a").on("click",function(){
        //询问用户确认导入
        var tipHtml = "";
        tipHtml += "<p>1、请先下载模板！</p>";
        tipHtml += "<p>2、文件格式必须与模板完全一致！</p>";
        tipHtml += "<p>3、填写了商品条码，可不填写商品编号、全名！</p>";
        tipHtml += "<p>4、没填写商品条码，商品编号为空时，商品全名不允许为空！！</p>";
        tipHtml += "<p>5、录入的商品条码、商品全名、商品编号等信息必须与系统中一致</p>";
        tipHtml += "<p>6、EXCEL中商品明细不能超过800行</p>";
        tipHtml += "<p>7、上传的商品明细文件必须为.xls/xlsx类型</p>";
        layer.confirm( tipHtml, {icon: 3, title:'导入规则说明'}, function( i){
            //do something
            layer.close( i);
            //触发文件上传按钮的点击时间
            $("#import_excel_btn").triggerHandler( "click");
        });
    });

    //excal模板下载
    $("#download_moden").on("click",function(){
        window.location.href = "/frenchProject/consumerImportExcel/consumerModel.xls";
    });

    //查询 账户总额信息
    $.get("/mooncardClient/getAccountTotal",function( res){
        if( res.code == 200){
            var total = res.data;
            getSystemParam( $, "mooncardBaseCount", function( data){
                var mooncardBaseCount = data;
                $("input[name=total_count]").val( (parseFloat( total) + ( mooncardBaseCount ? parseFloat( mooncardBaseCount) : 0.00)).toFixed(2));
            });

        }
    });

    //手动同步流水数据
    $("#sync_data_btn").click(function(){
        var index = layer.open({ type: 3});
        $.post("/mooncardClient/syncData",{},function( res){
            if( res.code == 200){
                tableIns.reload();
                layer.close( index);
                top.layer.msg( "同步交易流水成功");
            }else{
                top.layer.close( index);
                top.layer.msg( "同步交易流水失败");
            }
        });
    });
});
